﻿using Architecture.Repository.Interface.Traceability;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceability.Domain.TraceabilityDto;

namespace Traceability.Domain.Service.ShoppingServices
{
    public class ShoppingService : IShoppingService
    {
        /// <summary>
        /// 商品仓储
        /// </summary>
        protected readonly IShoppingRepository shoppingRepository;

        /// <summary>
        /// 商品名称仓储
        /// </summary>
        protected readonly IShoppingNameRepository shoppingNameRepository;

        /// <summary>
        /// 图片仓储
        /// </summary>
        protected readonly IPictureRepository pictureRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="shoppingRepository">商品仓储</param>
        /// <param name="shoppingNameRepository">商品名称仓储</param>
        /// <param name="pictureRepository">图片仓储</param>
        public ShoppingService(IShoppingRepository shoppingRepository, IPictureRepository pictureRepository, IShoppingNameRepository shoppingNameRepository)
        {
            this.shoppingRepository = shoppingRepository;
            this.pictureRepository = pictureRepository;
            this.shoppingNameRepository = shoppingNameRepository;
        }

        /// <summary>
        /// 添加商品信息
        /// </summary>
        /// <param name="addShoppingDto">商品Dto</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> AddShopping(AddShoppingDto addShoppingDto)
        {
            var img = new Pictures();
            img.PictureName=addShoppingDto.PictureName; //图片名称
            img.ProductionBatch=addShoppingDto.ProductionBatch; //生产批次
            img.CreatedBy=addShoppingDto.CreatedBy;
            img.CreatedDate=DateTime.Now;
            await pictureRepository.AddAsync(img);
            var list = await pictureRepository.GetAsync(x => x.PictureName == addShoppingDto.PictureName); //获取图片ID
            //if (list != null) { }
            var shopping = new Shopping();
            shopping.ProductionBatch = addShoppingDto.ProductionBatch;
            shopping.QGPeriod = addShoppingDto.QGPeriod;
            shopping.ProductionLicense = addShoppingDto.ProductionLicense;
            shopping.ManufacturingEnterprise = addShoppingDto.ManufacturingEnterprise;
            shopping.PictureId = list.FirstOrDefault().PictureId;
            shopping.ShoppingNameId = addShoppingDto.ShoppingNameId;
            shopping.CreatedBy = addShoppingDto.CreatedBy;
            shopping.CreatedDate = DateTime.Now;
            var res = await shoppingRepository.GetAsync(x => x.ShoppingId == addShoppingDto.ShoppingId);
            if (res.Count > 0)
            {
                return -2;//该公司名称已存在
            }
            return await shoppingRepository.AddAsync(shopping);
        }

        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <returns>返回商品列表</returns>
        public async Task<ShoppingPaginationDto<ShowShoppingDto>> GetShopping(int pageIndex, int pageSize)
        {
            var shopping = new ShoppingPaginationDto<ShowShoppingDto>();
            var list = (from a in await shoppingRepository.GetAllAsync()
                       join b in await shoppingNameRepository.GetAllAsync()
                       on a.ShoppingNameId equals b.ShoppingNameId
                       where !a.IsDeleted && !b.IsDeleted
                       select new ShowShoppingDto
                       {
                           ShoppingId = a.ShoppingId,
                           ProductionBatch = a.ProductionBatch,
                           QGPeriod = a.QGPeriod,
                           ProductionLicense = a.ProductionLicense,
                           ManufacturingEnterprise = a.ManufacturingEnterprise,
                           Information = a.Information,
                           PictureId = a.PictureId,
                           ShoppingNameId = a.ShoppingNameId,
                           CreatedBy = a.CreatedBy,
                           CreatedDate = a.CreatedDate,
                           UpdatedBy = a.UpdatedBy,
                           UpdatedDate = a.UpdatedDate,
                           DeletedBy = a.DeletedBy,
                           DeletedDate = a.DeletedDate,
                           IsDeleted = a.IsDeleted,
                           ShoppingNames=b.ShoppingNames
                       }).ToList();
            shopping.totalCount = list.Count;
            shopping.pageCount = (int)Math.Ceiling(list.Count * 1.0 / pageSize);
            shopping.list = list.OrderByDescending(x=>x.CreatedDate).Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();
            return shopping;
        }

        /// <summary>
        /// 删除商品信息
        /// </summary>
        /// <param name="shoppingId">商品ID</param>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public async Task<int> DeletedShopping(int shoppingId, string? userName)
        {
            var res = await shoppingRepository.GetModelAsync(shoppingId);
            res.IsDeleted = true;
            res.DeletedBy = userName;
            res.DeletedDate = DateTime.Now;
            return await shoppingRepository.UpdateAsync(res);
        }

        /// <summary>
        /// 反填商品实体
        /// </summary>
        /// <param name="shoppingId">商品ID</param>
        /// <returns>返回商品实体信息</returns>
        public async Task<ReverseShoppingDto> GetShoppingModel(int shoppingId)
        {
            var res = from a in await shoppingRepository.GetAllAsync()
                       join b in await pictureRepository.GetAllAsync()                       
                       on a.PictureId equals b.PictureId
                       join c in await shoppingNameRepository.GetAllAsync()
                       on a.ShoppingNameId equals c.ShoppingNameId
                       where a.ShoppingId == shoppingId
                       select new ReverseShoppingDto
                       {
                           ShoppingId = a.ShoppingId,
                           ProductionBatch = a.ProductionBatch,
                           QGPeriod = a.QGPeriod,
                           ProductionLicense = a.ProductionLicense,
                           ManufacturingEnterprise = a.ManufacturingEnterprise,
                           Information = a.Information,
                           PictureId = a.PictureId,
                           ShoppingNameId = a.ShoppingNameId,
                           CreatedBy=a.CreatedBy,
                           CreatedDate=a.CreatedDate,
                           UpdatedBy = a.UpdatedBy,
                           UpdatedDate = a.UpdatedDate,
                           DeletedBy=a.DeletedBy,
                           DeletedDate=a.DeletedDate,
                           IsDeleted=a.IsDeleted,                          
                           PictureName = b.PictureName,
                           ShoppingNames=c.ShoppingNames
                       };
            return res.FirstOrDefault();
        }

        /// <summary>
        /// 修改商品信息
        /// </summary>
        /// <param name="updateShoppingDto">商品DTO</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> UpdateShopping(UpdateShoppingDto updateShoppingDto)
        {
            var img = new Pictures();
            img.PictureId = updateShoppingDto.PictureId;
            img.PictureName= updateShoppingDto.PictureName;
            img.ProductionBatch= updateShoppingDto.ProductionBatch;
            img.UpdatedBy= updateShoppingDto.UpdatedBy;
            img.UpdatedDate=DateTime.Now;
            await pictureRepository.UpdateAsync(img);

            var res = await pictureRepository.GetAsync(x => x.PictureId == updateShoppingDto.PictureId);
            var shopping = new Shopping();
            shopping.ShoppingId = updateShoppingDto.ShoppingId;
            shopping.ProductionBatch = updateShoppingDto.ProductionBatch;
            shopping.QGPeriod = updateShoppingDto.QGPeriod;
            shopping.ProductionLicense = updateShoppingDto.ProductionLicense;
            shopping.ManufacturingEnterprise = updateShoppingDto.ManufacturingEnterprise;
            shopping.PictureId = res.FirstOrDefault().PictureId;
            shopping.ShoppingNameId = updateShoppingDto.ShoppingNameId;
            shopping.CreatedBy = updateShoppingDto.CreatedBy;
            shopping.CreatedDate =updateShoppingDto.CreatedDate;
            shopping.UpdatedDate = DateTime.Now;
            shopping.UpdatedBy = updateShoppingDto.UpdatedBy;
            shopping.DeletedBy=updateShoppingDto.DeletedBy;
            shopping.DeletedDate = updateShoppingDto.DeletedDate;
            shopping.IsDeleted= updateShoppingDto.IsDeleted;
            //var result = await shoppingRepository.GetAsync(x => x.ManufacturingEnterprise == updateShoppingDto.ManufacturingEnterprise);
            //if (result.Count > 0)
            //{
            //    return -2;//该公司名称已存在
            //}
            return await shoppingRepository.UpdateAsync(shopping);
        }
    }
}
